var params = "";
var sex = "";
var status="";
var codes = "";
var openId = "";
$(function(e){
	receiveData();
	dataInit();
});

function dataInit(){
	eventInit();
	openId = $.req("openId");
	codes = $.req("code");
	if((openId!=null && codes==null) || (openId != "" && codes=="") || (openId != undefined && codes==null)){//从其他页面跳转过来的
		$("#signModal").removeAttr("style");
	}else{
		//查询wechat_family表中是否存在该openID
		AjaxPostUtil.request({url : path+"1853f065ae4845f2a235af6449c18767", params:{codes:codes}, type : 'json', callback : function(json){
			console.log(json);
			if(json.returnCode == "-999"){
				openId = json.bean.openId;
				$("#signModal").removeAttr("style");
			}else if(json.returnCode == "-888"){
				 //关闭当前页面，返回到微信主页面
				swal({ title : "提示", text : "信息正在审核中...", type : "warning" ,closeOnConfirm:false, closeOnCancel:false },function(){
					wx.closeWindow();
				});
			}else if(json.returnCode == "-777"){
				swal({ title : "提示", text : "信息审核失败，请重新登记！", type : "warning"});
				openId = json.bean.openId;
				$("#signModal").removeAttr("style");
			}else if(json.returnCode == 0){
				$("#signModal").attr("style","display: none;");
				$("#successModal").removeAttr("style");
				$("#successInfo").val(json.bean.hInfo);
				
			}else{
				swal({ title : "警告", text : "请重新打开页面", type : "warning" ,closeOnConfirm:false, closeOnCancel:false },function(){
					wx.closeWindow();
			}); 
			}
			
		}});
	}
	
}

function toLogin(){
	if(isNull($("#hInfo").val())){
		swal({ title : "请填写居住地址!", text : "", type : "error" }); 
		return;
	}else if(isNull($("#name").val())){
		swal({ title : "请填写姓名!", text : "", type : "error" }); 
		return;
	}else if(isNull($("#phone").val())|| checkMobile($("#phone").val())==false){
		swal({ title : "请正确填写手机号！", text : "", type : "error" }); 
		return;
	}else if(isNull($("#sex").val())){
		swal({ title : "请填写性别！", text : "", type : "error" }); 
		return;
	}else if(isNull($("#status").val())){
		swal({ title : "请填写您的身份！", text : "", type : "error" }); 
		return;
	}else{
		
		var roomString = $("#room").val();
		var a = roomString.indexOf("-");
		var roomId = parseInt(roomString.substr(0,a));//房间ID
		params = {
	    		roomNum : roomId,
				familyName : $("#name").val(),
				familyPhone : $("#phone").val(),
				familySex : $("#sex").val(),
				familyStatus : $("#status").val(),
				openId : openId,
				codes : codes,
				hInfo : $("#hInfo").val()
		}
		AjaxPostUtil.request({url : path+"4aeec18d9f674daa89d5c2307418c694", params : params, type : 'json', callback : function(json){
			if(json.returnCode == 0){
				
				$("#signModal").attr("style","display: none;");
				$("#successModal").attr("style","display: none;");
				swal({ title : "成功", text : "提交成功！", type : "success" ,closeOnConfirm:false, closeOnCancel:false }, function(){
					wx.closeWindow();
				});
			}else{
				swal({ title : "失败", text : json.returnMessage, type : "error" }); 
			}
			
		}});
		
	}
}

function selectAddress(){
	if(isNull($("#build").val()) || $("#build").val()== '请选择楼号' ){
		swal({ title : "请填写楼号！", text : "", type : "error" }); 
		return;
	}else if(isNull($("#unit").val()) || $("#unit").val()== '请选择单元号'){
		swal({ title : "请填写单元号！", text : "", type : "error" }); 
		return;
	}else if(isNull($("#room").val())){
		swal({ title : "请填写房间号！", text : "", type : "error" }); 
		return;
	}else{
	
		var params=null;
		var roomString = $("#room").val();
		var a = roomString.indexOf("-");
		var roomId = parseInt(roomString.substr(0,a));//房间ID
		var roomName = roomString.substr(a+1);//房间名字
		params = {
			
			buildId:$("#build").val(),
			unit:$("#unit").val(),
			roomNum:roomId
		};
		var address=params.buildId+"号楼"+params.unit+"单元"+roomName+"室";
		$("#hInfo").val(address);
		$("#selectAddress").modal("hide");
	}
}




function eventInit(){
	//选择地址
	$('body').on('click', '#hInfo', function(e){
		$("#selectAddress").modal("show");
	});	
	$('body').on('click', '#addressBtn', function(e){
		selectAddress();
	});	
	
	//登记注册
	$('body').on('click', '#loginBtn', function(e){
		toLogin();
	});	
	
	//选择性别
	$('body').on('click', '#nan', function(e){
		var sex=$(this).val();
		$(this).css("background-color","#03BB9B");
		$("#nv").removeAttr("style");
		$("#sex").val(sex);
	});	
	$('body').on('click', '#nv', function(e){
		var sex=$(this).val();
		$(this).css("background-color","#03BB9B");
		$("#nan").removeAttr("style");
		$("#sex").val(sex);
	});
	
	//选择身份
	$('body').on('click', '#yezhu', function(e){
		var status=$("#yezhu").val();
		$(this).css("background-color","#03BB9B");
		$("#jiashu").removeAttr("style");
		$("#zuke").removeAttr("style");
		$("#status").val(status);
		
	});	
	$('body').on('click', '#jiashu', function(e){
		var status=$("#jiashu").val();
		$(this).css("background-color","#03BB9B");
		$("#yezhu").removeAttr("style");
		$("#zuke").removeAttr("style");
		$("#status").val(status);
	});	
	$('body').on('click', '#zuke', function(e){
		var status=$("#zuke").val();
		$(this).css("background-color","#03BB9B");
		$("#yezhu").removeAttr("style");
		$("#jiashu").removeAttr("style");
		$("#status").val(status);
	});	
	
	
	//选择楼号后的改变事件，选择楼号后出现单元号的选择框
	$('body').on('change', '#build',function(e){
		var buildID = $(this).val();
		if(buildID !=null || buildID != '' || buildID != '请选择楼号'){
			$("#unitDiv").removeClass("hide");
		}else if(buildID == '请选择楼号'){
			$("#unitDiv").addClass("hide");
		}
		var json = {"total":0,"returnCode":0,"bean":"","returnMessage":"成功","rows":[{"unit":"请选择单元号"},{"unit":"1"},{"unit":"2"}]};
		
		var source = $("#selectUnit").html();
		var template = Handlebars.compile(source);
		$("#unit").html(template(json));
	});
	//选择单元号后的改变事件，选择楼层号后出现房间号的选择框
	$('body').on('change','#unit', function(e){
		var unitID = $(this).val();
		if(unitID !=null || unitID != '' || unitID != '请选择单元号' ){
			$("#roomDiv").removeClass("hide");
		}else{
			$("#roomDiv").addClass("hide");
		}
		var params = {
				buildId : $("#build").val(),
				unit : $("#unit").val(),
				//floor : $("#floor").val()
		}
		AjaxPostUtil.request({url : path+"5033f16d3b9d4f82a144bc58361dfc51", params : params, type : 'json', callback : function(json){
			
			if(json.returnCode == 0){
				console.log(json);
				var source = $("#selectRoom").html();
				var template = Handlebars.compile(source);
				$("#room").html(template(json));
				
			}else{
				qiao.bs.msg({msg : json.returnMessage, type : 'danger'});
			}
		}})
		
	});
	
	//登记新的房屋
	$('body').on('click','#successBtn',function(e){
		$("#signModal").removeAttr("style");
	});
}
